package com.czd.support.controller;

import com.czd.support.model.DataDictionary;
import com.czd.support.service.DataDictionaryService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

 
/**
 * @Description  TODO
 * @Author JinMa.Xiao
 * @Date    2015年5月13日 下午3:43:50
 * @Version v1.0
 */
@Controller
@RequestMapping(  "/datadictionary")
public class DataDictionaryController extends BaseController {

    @Autowired
    private DataDictionaryService dataDictionaryService;

    /**
     * 答案列表页
     * @param model
     * @param request
     * @param answers
     * @return
     */
    @RequestMapping(value = {"","/","list"},method = RequestMethod.GET)
    public String list(Model model,HttpServletRequest request,DataDictionary dataDictionary,@RequestParam(value = "pageNo",defaultValue = "1") int pageNo){
    	dataDictionary.setPageNo(pageNo);
        model.addAttribute("paginate",dataDictionaryService.queryForPaginate(dataDictionary));
        dataDictionaryService.queryForPaginate(dataDictionary);
        return "datadictionary/list";//跳到页面
    }

    
    /**
     * 新增数据字典
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(){
        return "datadictionary/add";//跳到页面   //不变网址
    }
    
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String save(DataDictionary dataDictionary,HttpServletRequest request,  Model model){
    	String code = dataDictionary.getCode();  
    	
    	  DataDictionary findById = dataDictionaryService.findById(Integer.parseInt(code)
    			  );
    	  if(findById!=null){//已经存在
    		   return "datadictionary/add";//跳到页面   //不变网址
    		  
    	  }else{
    	  dataDictionaryService.insertdataDictionary(dataDictionary);
           return "redirect:/datadictionary/list";//跳到新请求    //不变网址
    	  }
    }  
    
    /**
     * 删除code  
     */
    @RequestMapping(value = "/{id}/del",method = RequestMethod.GET)
    public String del(@PathVariable int id,HttpServletRequest request){
    	 DataDictionary dataDictionary = new DataDictionary();
    	 dataDictionary.setCode(id+"");
    	dataDictionaryService.del(dataDictionary);
    	return  "redirect:/datadictionary/list"  ;//跳到新请求   
    	 
    }
    
    
    /**
     * 编辑
     */
    @RequestMapping(value = "/{id}/edit",method = RequestMethod.GET)
    public String change(@PathVariable int id,Model model){
       DataDictionary dataDictionary = dataDictionaryService.findById(id);
       model.addAttribute("dataDictionary",dataDictionary);
       return "datadictionary/edit";//跳到页面    //变网址
    }
    
    /**
     *编辑保存   //校验code不能存在  ...存在则不保存 ...不存在则保存
     */
    @RequestMapping(value = "/{code}/edit",method = RequestMethod.POST)
    public String save(@PathVariable int code  , DataDictionary dataDictionary,HttpServletRequest request){
           System.out.println("老old"+code);   
        String newcode = dataDictionary.getCode();
        int parseInt = Integer.parseInt(newcode);
    	    DataDictionary ww = dataDictionaryService.findById(parseInt);
    	       if(ww!=null&(!(code+"").equals(newcode))) {  //  
    	    		  return  "redirect:/datadictionary/list"  ;
    	      }else{
    		   DataDictionary dataDictionary1  =  new  DataDictionary();
    		   dataDictionary1.setCode(code+"");   
    		   dataDictionaryService.del(dataDictionary1);
    		   dataDictionaryService.insertdataDictionary(dataDictionary);
    	      }
    	  return  "redirect:/datadictionary/list"  ;//跳到新请求   不变网址
    	  
    }

      

}
